package com.qf.controller;

import com.qf.pojo.req.TbRolePermissionReq;
import com.qf.pojo.req.TbRoleReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.TbRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api("角色控制器")
@RestController
@RequestMapping("/role")
public class TbRoleController {
    @Autowired
    TbRoleService tbRoleService;
    @RequiresPermissions({"ROLE_FIND_ALL"})
    @ApiOperation("查询所有角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "当前页显示多少行数据",required = true,dataType = "Integer")
    })
    @GetMapping("/findAll")
    public BaseResp findAll(@RequestParam("page")Integer page,@RequestParam("size")Integer size){
        return  tbRoleService.findAll(page,size);
    }
    @RequiresPermissions({"ROLE_UPDATE_OR_SAVE"})
    @ApiOperation("修改或新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbRoleReq",value = "角色请求对象",required = true,dataType = "TbRoleReq")
    })
    @PostMapping("/updateOrSave")
    public BaseResp updateOrSave(@RequestBody TbRoleReq tbRoleReq){
        return tbRoleService.updateOrSave(tbRoleReq);
    }
    @RequiresPermissions({"ROLE_DELETE"})
    @ApiOperation("删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbRoleReq",value = "角色请求对象",required = true,dataType = "TbRoleReq")
    })
    @PostMapping("/deleteById")
    public BaseResp deleteById(@RequestBody TbRoleReq tbRoleReq){
        return tbRoleService.deleteById(tbRoleReq.getId());
    }
    @ApiOperation("通过id查询该角色的权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map",value = "角色id",required = true,dataType = "Map")
    })
    @PostMapping("/findPermissionByRoleId")
    public BaseResp findPermissionByRoleId(@RequestBody Map map){
        return tbRoleService.findPermissionByRoleId(Integer.valueOf(map.get("id").toString()));
    }
    @ApiOperation("修改或增加角色的权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbRolePermissionReq",value = "角色权限",required = true,dataType = "TbRolePermissionReq")
    })
    @PostMapping("/updateRolePermission")
    public BaseResp updateRolePermission(@RequestBody TbRolePermissionReq tbRolePermissionReq){
        return tbRoleService.updateRolePermission(tbRolePermissionReq);
    }

}
